﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Point = System.Windows.Point;
using UserControl = System.Windows.Controls.UserControl;

namespace weighing
{
    /// <summary>
    /// DragLineUc.xaml 的交互逻辑
    /// </summary>
    public partial class DragLineUc : UserControl
    {
        private Canvas canvas;
        private bool isDragging = false;
        private Point mouseOffset;
        public bool isCanDrag =true;
        public Thickness MainBorderThickness { get; set; } = new Thickness(0, 1, 0, 0);
        public double MainBorderWidth { get; set; } = 100;
        public double MainBorderHeight { get; set; } = 5;

        public Border MainBorder
        {
            get
            {
                return this.Content as Border;
            }
        }

        public DragLineUc()
        {
            InitializeComponent();
            this.Loaded += DragUc_Loaded;
            this.Unloaded += DragUc_Unloaded;
            this.DataContext = this;
            this.Clip = null;
        }
        #region user content event

        private void DragUc_Loaded(object sender, RoutedEventArgs e)
        {
            this.MouseLeftButtonDown += DragUc_MouseLeftButtonDown; ;
            this.MouseLeftButtonUp += DragUc_MouseLeftButtonUp; ;
            this.MouseMove += DragUc_MouseMove; ;
            this.KeyUp += DragUc_KeyUp;
        }

        private void DragUc_Unloaded(object sender, RoutedEventArgs e)
        {
            this.MouseLeftButtonDown -= DragUc_MouseLeftButtonDown;
            this.MouseLeftButtonUp -= DragUc_MouseLeftButtonUp;
            this.MouseMove -= DragUc_MouseMove;
            this.KeyUp -= DragUc_KeyUp;
        }
        #endregion

        #region drag event
        private void DragUc_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            canvas ??= VisualTreeHelper.GetParent(this) as Canvas;
            if (canvas != null)
            {
                isDragging = true;
                mouseOffset = e.GetPosition(this);
                this.CaptureMouse();
            }
        }

        private void DragUc_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isDragging)
            {
                this.ReleaseMouseCapture();
                isDragging = false;
            }
        }
        private void DragUc_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (isCanDrag == false) return;
            if (canvas != null && isDragging == true)
            {
                Point p = e.GetPosition(canvas);
                this.SetValue(Canvas.TopProperty, p.Y - mouseOffset.Y);
                this.SetValue(Canvas.LeftProperty, p.X - mouseOffset.X);
            }
        }
        private void DragUc_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            this.CaptureMouse();
            switch (e.Key)
            {
                case Key.Left:
                    this.SetValue(Canvas.LeftProperty, Canvas.GetLeft(this) - 1);
                    break;
                case Key.Up:
                    this.SetValue(Canvas.TopProperty, Canvas.GetTop(this) - 1);
                    break;
                case Key.Right:
                    this.SetValue(Canvas.LeftProperty, Canvas.GetLeft(this) + 1);
                    break;
                case Key.Down:
                    this.SetValue(Canvas.TopProperty, Canvas.GetTop(this) + 1);
                    break;
            }
            this.ReleaseMouseCapture();
        }
        #endregion

        public void SetBorderThickness(Thickness thickness)
        {
            MainBorder.BorderThickness = thickness;
        }
    }
}
